Imports Microsoft.VisualBasic
Imports System.Data.Sql
Imports System.Data.SqlClient
Imports System
Imports System.Data

Imports GTSDALTableAdapters
Imports GTSDAL
Imports GTSSystem.BLL.IdGenerator
Imports GTSSystem.BLL.Location
Imports GTSSystem.BLL.ContactInfo

Namespace GTSSystem.BLL
    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks>
    ''' TODO: lookupShipmetItems by reference number
    ''' </remarks>
    <System.ComponentModel.DataObject()> _
Public Class ShipmentItems

        Private _ShipmentItemsAdaptor As ShipmentItemTableAdapter = Nothing
        Protected ReadOnly Property ShipmentItemsAdaptor() As ShipmentItemTableAdapter
            Get
                If _ShipmentItemsAdaptor Is Nothing Then
                    _ShipmentItemsAdaptor = New ShipmentItemTableAdapter
                End If
                Return _ShipmentItemsAdaptor
            End Get
        End Property
        Public Sub New()
            'Constructor Code here
        End Sub

        <System.ComponentModel.DataObjectMethod(ComponentModel.DataObjectMethodType.Insert, True)> _
        Public Function AddShipmentItem(ByVal ReferenceNumber As Integer, _
                                        ByVal description As String, _
                                        ByVal Weight As Double, _
                                        ByVal Length As Double, _
                                        ByVal Height As Double, _
                                        ByVal Width As Double) As Integer
            Dim ShipmentItemsPK As Integer

            Try
                ShipmentItemsPK = GetNextId("ShipmentDetail")
                AddShipmentItem = ShipmentItemsAdaptor.Insert(ShipmentItemsPK, ReferenceNumber, description, Weight, Length, Height, Width)
                Dim Shipmentmgr As New GTSSystem.BLL.Shipment
                Dim ItemsMgr As New GTSSystem.BLL.ShipmentItems
                Dim TotalItems As Integer = ItemsMgr.getShipmentTotalItems(ReferenceNumber)
                Dim TotalWeight As Double = ItemsMgr.getShipmentTotalWeight(ReferenceNumber)
                Shipmentmgr.UpdateItemsandWeightShipment(ReferenceNumber, TotalItems, TotalWeight)
            Catch ex As Exception
                Throw
            End Try
        End Function
        <System.ComponentModel.DataObjectMethod(ComponentModel.DataObjectMethodType.Update, True)> _
        Public Function UpdateShipmentItem(ByVal ShipmentitemId As Integer, _
                                            ByVal ReferenceNumber As Integer, _
                                            ByVal description As String, _
                                          ByVal Weight As Double, _
                                            ByVal Length As Double, _
                                         ByVal Height As Double, _
                                            ByVal Width As Double) As Integer


            Try
                UpdateShipmentItem = ShipmentItemsAdaptor.Update(ShipmentitemId, ReferenceNumber, description, Weight, Length, Height, Width)
                Dim Shipmentmgr As New GTSSystem.BLL.Shipment
                Dim ItemsMgr As New GTSSystem.BLL.ShipmentItems
                Dim TotalItems As Integer = ItemsMgr.getShipmentTotalItems(ReferenceNumber)
                Dim TotalWeight As Double = ItemsMgr.getShipmentTotalWeight(ReferenceNumber)
                Shipmentmgr.UpdateItemsandWeightShipment(ReferenceNumber, TotalItems, TotalWeight)
            Catch ex As Exception
                Throw
            End Try

        End Function
        <System.ComponentModel.DataObjectMethod(ComponentModel.DataObjectMethodType.Select, True)> _
       Public Function getShipmentTotalItems(ByVal ReferenceNumber As Integer) As Integer
            Try
               
                getShipmentTotalItems = ShipmentItemsAdaptor.GetTotalItemsByReferenceNumber(ReferenceNumber)
            Catch ex As Exception
                Throw
            End Try
        End Function
        <System.ComponentModel.DataObjectMethod(ComponentModel.DataObjectMethodType.Select, True)> _
        Public Function getShipmentTotalWeight(ByVal ReferenceNumber As Integer) As Double
            Try
                getShipmentTotalWeight = ShipmentItemsAdaptor.GetTotalWeightByReferenceNumber(ReferenceNumber)
            Catch ex As Exception
                Throw
            End Try
        End Function


            
        <System.ComponentModel.DataObjectMethod(ComponentModel.DataObjectMethodType.Select, True)> _
           Public Function getAllShipmentItems() As ShipmentItemDataTable
            Try
                getAllShipmentItems = ShipmentItemsAdaptor.GetShipmentItem()
            Catch ex As Exception
                Throw
            End Try
        End Function
        <System.ComponentModel.DataObjectMethod(ComponentModel.DataObjectMethodType.Select, True)> _
          Public Function getAllShipmentItemsByReferenceNumber(ByVal ReferenceNumber As Integer) As ShipmentItemDataTable
            Try
                getAllShipmentItemsByReferenceNumber = ShipmentItemsAdaptor.GetShipmentItemByReferenceNumber(ReferenceNumber)
            Catch ex As Exception
                Throw
            End Try
        End Function
        <System.ComponentModel.DataObjectMethod(ComponentModel.DataObjectMethodType.Delete, True)> _
            Public Function deleteShipmentItem(ByVal ShipmentItemId As Integer) As Integer
            Try
                deleteShipmentItem = ShipmentItemsAdaptor.Delete(ShipmentItemId)
                
            Catch ex As Exception
                Throw
            End Try
        End Function


    End Class

End Namespace



